//
//  SingleBreath.m
//
//  Created by Eric van Riet Paap on 13-09-08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import <CoreFoundation/CoreFoundation.h>

#import "SingleBreath.h"
#import "trainingViewController.h"
#import "historyViewController.h"
#import "settingsViewController.h"
#import "helpViewController.h"


@implementation SingleBreath


@synthesize	window, myTrainingViewController, myHistoryViewController, mySettingsViewController, myHelpViewController, database, trainingHistory;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
	[self createEditableCopyOfDatabaseIfNeeded];
	[self connectToDatabase];
	[self loadTrainingHistoryFromDatabase];
	
	window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

	myTrainingViewController = [[[trainingViewController alloc] init] retain];
	[myTrainingViewController setDatabase:database];
	
	myHistoryViewController  = [[[historyViewController  alloc] init] retain];
	mySettingsViewController = [[[settingsViewController alloc] init] retain];
	myHelpViewController     = [[[helpViewController     alloc] init] retain];
	NSArray*	viewControllers = [[NSArray arrayWithObjects:
									myTrainingViewController, 
									//myHistoryViewController, 
									//mySettingsViewController, 
									myHelpViewController, 
									nil] retain];
	UITabBarController	*tabbar = [[[UITabBarController alloc] init] retain];
	[tabbar setViewControllers:viewControllers animated:NO];
	//tabbar.view.alpha = 0.5; //hmm, this dims the entire view (logical probably, but I wanted to make the bar itself translucent)?!?
    [window addSubview:tabbar.view];

	[window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
	[self disconnectFromDatabase];
}


- (void)dealloc {
    [myTrainingViewController release];
    [myHistoryViewController release];
    [mySettingsViewController release];
    [myHelpViewController release];
	[window release];
	[super dealloc];
}


#pragma mark -
#pragma mark Database access

- (BOOL)createEditableCopyOfDatabaseIfNeeded {
    // See if the database already exists
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"singlebreath.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) {
		NSLog(@"Using already existing singlebreath.sqlite");
		return NO;
	}
	
    // The database does not exist, so copy a blank starter database to the Documents directory
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"singlebreath.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
	NSLog(@"Copied singlebreath.sqlite");
	return YES;
}


- (void)connectToDatabase {
	// The database is stored in the application bundle. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"singlebreath.sqlite"];
    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
		NSLog(@"Connected to database");
	} else {
        // Even though the open failed, call close to properly clean up resources.
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }
}

- (void)disconnectFromDatabase {
	// TODO: Maybe write out all database entries to disk
	//	[books makeObjectsPerformSelector:@selector(dehydrate)];
	//SQLSQL [SLSMolecule finalizeStatements];
    // Close the database.
    if (sqlite3_close(database) == SQLITE_OK)  {
		NSLog(@"Disconnected from database");
	} else {
        NSAssert1(0, @"Error: failed to close database with message '%s'.", sqlite3_errmsg(database));
    }
}


/*
- (void)loadInitialMoleculesFromDisk;
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	rootViewController.molecules = nil;
	
	
	if ([self createEditableCopyOfDatabaseIfNeeded])
	{
		// The database needed to be recreated, so scan and copy over the default files
		[self performSelectorOnMainThread:@selector(showStatusIndicator) withObject:nil waitUntilDone:NO];
		
		[self connectToDatabase];
		// Before anything else, move included PDB files to /Documents if the program hasn't been run before
		// User might have intentionally deleted files, so don't recopy the files in that case
		NSError *error = nil;
		// Grab the /Documents directory path
		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
		NSString *documentsDirectory = [paths objectAtIndex:0];
		
		NSFileManager *fileManager = [NSFileManager defaultManager];
		// Iterate through all files sitting in the application's Resources directory
		// TODO: Can you fast enumerate this?
		NSDirectoryEnumerator *direnum = [fileManager enumeratorAtPath:[[NSBundle mainBundle] resourcePath]];
		NSString *pname;
		while (pname = [direnum nextObject])
		{
			if ([[pname pathExtension] isEqualToString:@"gz"])
			{
				NSString *preloadedPDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pname];
				NSString *installedPDBPath = [documentsDirectory stringByAppendingPathComponent:pname];
				if (![fileManager fileExistsAtPath:installedPDBPath])
				{
					// Move included PDB files to /Documents
					[[NSFileManager defaultManager]	copyItemAtPath:preloadedPDBPath toPath:installedPDBPath error:&error];
					if (error != nil)
					{
						//						NSLog(@"Failed to copy over PDB files with error: '%@'.", [error localizedDescription]);
						// TODO: Report the file copying problem to the user or do something about it
					}
				}
				
			}
		}
		
		[self loadMissingMoleculesIntoDatabase];
		
		[[NSUserDefaults standardUserDefaults] synchronize];		
		[self performSelectorOnMainThread:@selector(hideStatusIndicator) withObject:nil waitUntilDone:NO];
	}
	else
	{
		// The MySQL database has been created, so load molecules from the database
		[self connectToDatabase];
		// TODO: Check to make sure that the proper version of the database is installed
		[self loadAllMoleculesFromDatabase];
		[self loadMissingMoleculesIntoDatabase];		
	}
	
	rootViewController.database = database;
	rootViewController.molecules = molecules;
	
	[rootViewController loadInitialMolecule];
	[pool release];
}
*/


- (void)loadTrainingHistoryFromDatabase {
	NSLog(@"loadTrainingHistoryFromDatabase");
	
	trainingHistory = [[NSMutableArray alloc] init];
	
	const char *sql = "SELECT * FROM training_result"; 	//id INTEGER PRIMARY KEY, training_id INTEGER, timestamp REAL, percent_finished REAL, comments TEXT

	sqlite3_stmt *loadingStatement;
	
	if (sqlite3_prepare_v2(database, sql, -1, &loadingStatement, NULL) == SQLITE_OK) 
	{
		while (sqlite3_step(loadingStatement) == SQLITE_ROW) 
		{
			int		_id              = sqlite3_column_int(loadingStatement, 0);
			int		training_id      = sqlite3_column_int(loadingStatement, 1);
			double	timestamp        = sqlite3_column_double(loadingStatement, 2);
			double	percent_finished = sqlite3_column_double(loadingStatement, 3);
			char*	_comments        = (char*)sqlite3_column_text(loadingStatement, 4);
			NSString *comments =  (_comments) ? [NSString stringWithUTF8String:_comments]  : @"";
			NSLog([NSString stringWithFormat:@"    id=%d, training_id=%d, timstamp=%f, percent_finished=%f, comments=%@",
				_id, training_id, timestamp, percent_finished, comments]);
			//[trainingHistory addObject:timestamp];
		}
	}
	// "Finalize" the statement - releases the resources associated with the statement.
	sqlite3_finalize(loadingStatement);	
}


/*
- (void)loadMissingMoleculesIntoDatabase {
	// First, load all molecule names from the database
	NSMutableDictionary *moleculeFilenameLookupTable = [[NSMutableDictionary alloc] init];
	
	const char *sql = "SELECT * FROM molecules";
	sqlite3_stmt *moleculeLoadingStatement;
	
	if (sqlite3_prepare_v2(database, sql, -1, &moleculeLoadingStatement, NULL) == SQLITE_OK) 
	{
		while (sqlite3_step(moleculeLoadingStatement) == SQLITE_ROW) 
		{
			char *stringResult = (char *)sqlite3_column_text(moleculeLoadingStatement, 1);
			NSString *sqlString =  (stringResult) ? [NSString stringWithUTF8String:stringResult]  : @"";
			NSString *filename = [sqlString stringByReplacingOccurrencesOfString:@"''" withString:@"'"];
			[moleculeFilenameLookupTable setValue:[NSNumber numberWithBool:YES] forKey:filename];
		}
	}
	sqlite3_finalize(moleculeLoadingStatement);	
	
	// Now, check all the files on disk to see if any are missing from the database
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager]
									  enumeratorAtPath:documentsDirectory];
	NSString *pname;
	while (pname = [direnum nextObject])
	{
		if ( ([moleculeFilenameLookupTable valueForKey:pname] == nil) && ([[pname pathExtension] isEqualToString:@"gz"]) )
		{
			// Parse the PDB file into the database
			SLSMolecule *newMolecule = [[SLSMolecule alloc] initWithFilename:pname database:database];
			if (newMolecule != nil)
				[molecules addObject:newMolecule];
			[newMolecule release];			
		}
	}
	
	[moleculeFilenameLookupTable release];
}
*/


@end
